providers.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. class AbstractProvider(object):
  2. """Delegate class to provide requirement interface for the resolver.
  3. """
  4. def identify(self, dependency):
  5. """Given a dependency, return an identifier for it.
  6. This is used in many places to identify the dependency, e.g. whether
  7. two requirements should have their specifier parts merged, whether
  8. two specifications would conflict with each other (because they the
  9. same name but different versions).
  10. """
  11. raise NotImplementedError
  12. def get_preference(self, resolution, candidates, information):
  13. """Produce a sort key for given specification based on preference.
  14. The preference is defined as "I think this requirement should be
  15. resolved first". The lower the return value is, the more preferred
  16. this group of arguments is.
  17. :param resolution: Currently pinned candidate, or `None`.
  18. :param candidates: A list of possible candidates.
  19. :param information: A list of requirement information.
  20. Each information instance is a named tuple with two entries:
  21. * `requirement` specifies a requirement contributing to the current
  22. candidate list
  23. * `parent` specifies the candidate that provides (dependend on) the
  24. requirement, or `None` to indicate a root requirement.
  25. The preference could depend on a various of issues, including (not
  26. necessarily in this order):
  27. * Is this package pinned in the current resolution result?
  28. * How relaxed is the requirement? Stricter ones should probably be
  29. worked on first? (I don't know, actually.)
  30. * How many possibilities are there to satisfy this requirement? Those
  31. with few left should likely be worked on first, I guess?
  32. * Are there any known conflicts for this requirement? We should
  33. probably work on those with the most known conflicts.
  34. A sortable value should be returned (this will be used as the `key`
  35. parameter of the built-in sorting function). The smaller the value is,
  36. the more preferred this specification is (i.e. the sorting function
  37. is called with `reverse=False`).
  38. """
  39. raise NotImplementedError
  40. def find_matches(self, requirements):
  41. """Find all possible candidates that satisfy the given requirements.
  42. This should try to get candidates based on the requirements' types.
  43. For VCS, local, and archive requirements, the one-and-only match is
  44. returned, and for a "named" requirement, the index(es) should be
  45. consulted to find concrete candidates for this requirement.
  46. :param requirements: A collection of requirements which all of the the
  47. returned candidates must match. All requirements are guaranteed to
  48. have the same identifier. The collection is never empty.
  49. :returns: An iterable that orders candidates by preference, e.g. the
  50. most preferred candidate should come first.
  51. """
  52. raise NotImplementedError
  53. def is_satisfied_by(self, requirement, candidate):
  54. """Whether the given requirement can be satisfied by a candidate.
  55. The candidate is guarenteed to have been generated from the
  56. requirement.
  57. A boolean should be returned to indicate whether `candidate` is a
  58. viable solution to the requirement.
  59. """
  60. raise NotImplementedError
  61. def get_dependencies(self, candidate):
  62. """Get dependencies of a candidate.
  63. This should return a collection of requirements that `candidate`
  64. specifies as its dependencies.
  65. """
  66. raise NotImplementedError
  67. class AbstractResolver(object):
  68. """The thing that performs the actual resolution work.
  69. """
  70. base_exception = Exception
  71. def __init__(self, provider, reporter):
  72. self.provider = provider
  73. self.reporter = reporter
  74. def resolve(self, requirements, **kwargs):
  75. """Take a collection of constraints, spit out the resolution result.
  76. This returns a representation of the final resolution state, with one
  77. guarenteed attribute ``mapping`` that contains resolved candidates as
  78. values. The keys are their respective identifiers.
  79. :param requirements: A collection of constraints.
  80. :param kwargs: Additional keyword arguments that subclasses may accept.
  81. :raises: ``self.base_exception`` or its subclass.
  82. """
  83. raise NotImplementedError